/**
 * com.dingjian.basedata.org.controller.OrgEditController.java
 */
package com.dingjian.basedata.org.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dingjian.base.dao.QueryExecutor;
import com.dingjian.base.service.BaseService;
import com.dingjian.base.util.StringUtils;
import com.dingjian.basedata.org.model.Org;
import com.dingjian.basedata.org.service.OrgService;
import com.dingjian.framework.base.controller.EditController;

/**
 * @author FengMy
 * 
 * @since 2012-9-10
 */
@Controller
@RequestMapping(value="basedata/org/*")
public class OrgEditController extends EditController {
	@Resource(name="queryExecutor")
	protected QueryExecutor queryExecutor;
	@Autowired
	private OrgService orgService;
	@Override
	protected Class<Org> getSubmitClass() {
		return Org.class;
	}

	@Override
	protected BaseService<Org> getService() {
		return orgService;
	}
	
	@Override
	protected boolean validate(Object data) {
		Org entity = (Org) data;
		if(StringUtils.isEmpty(entity.getName())){
			getOutputMsg().put("MSG", "名称不能为空");
			return false;
		}
		if(StringUtils.isEmpty(entity.getNumber())){
			getOutputMsg().put("MSG", "编码不能为空");
			return false;
		}
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("number", entity.getNumber());
		List<Org> values = queryExecutor.execQuery("com.dingjian.basedata.org.dao.OrgDao.select", param, Org.class);
		for(Org g : values){
			if(!g.getId().equals(entity.getId())){
				getOutputMsg().put("MSG", "该编码己存在");
				return false;
			}
		}
		param.clear();
		param.put("name", entity.getName());
		values = queryExecutor.execQuery("com.dingjian.basedata.org.dao.OrgDao.select", param, Org.class);
		for(Org g : values){
			if(!g.getId().equals(entity.getId())){
				getOutputMsg().put("MSG", "该名称己存在");
				return false;
			}
		}
 		return super.validate(data);
	}

}
